using Digitalmes.Application.Sys.Posts;
using Digitalmes.Application.Sys.Posts.Commands;
using Digitalmes.Application.Sys.Posts.Queries;

namespace Digitalmes.WebApi.Endpoints.Sys;

/// <summary>
/// 职位终结点。
/// </summary>
public sealed class PosEndPoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "sys", "pos")
            .RequireAuthorization()
            .MapGet(GetPosListAsync, "list")
            .MapGet(GetPosPagedListAsync, "list-page")
            .MapPost(CreatePosAsync)
            .MapPut(UpdatePosAsync, "{id}")
            .MapDelete(DeletePosAsync, "{id}");
    }

    [EndpointDescription("获取职位集合")]
    public async Task<IApiResult> GetPosListAsync(ISender sender, [AsParameters] GetPosListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页获取职位")]
    public async Task<IApiResult> GetPosPagedListAsync(ISender sender, [AsParameters] GetPosListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建职位")]
    public async Task<IApiResult> CreatePosAsync(ISender sender, SysPosDto dto)
    {
        var result = await sender.Send(new CreatePosCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("更新职位信息")]
    public async Task<IApiResult> UpdatePosAsync(ISender sender, long id, SysPosDto dto)
    {
        var result = await sender.Send(new UpdatePosCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除职位")]
    public async Task<IApiResult> DeletePosAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeletePosCommand(id));
        return result.ToApiResult();
    }
}
